<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\Auxi;
use App\Tools\Html;
use App\Tools\Spell;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Index {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Repository\Archives $repoArc,
                                Repository\Category $category) {}

    public function index() {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aryAdPc'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 0);
            $_model['aryAdMobile'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 1);

            $_model['homeNews'] = $this->repoArc->findHomeNews(5, $this->__LANGUAGE_ID__, 0, 15);
            $_model['product'] = $this->repoArc->findHomeNews(0, $this->__LANGUAGE_ID__, 0, 15);
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['pageSeoTitle'] = 'Bondent Group';
            }

            //首页底部链接
            return array (
                'model' => $_model,
                'view' => true
            );
        }
        return 404;
    }

    public function category($__Route = array('/*/{aliasId}/{page:\d*}', '/category/{aliasId}/{page}')) {
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aliasId'] = $this->repoArc->aliasId($this->__LANGUAGE_ID__, $this->aliasId);
            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);

            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(20))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else if ($_model['channelType'] == 1) {
                $user = $this->session->member;
                if (!$user) {
                    $user['member_id'] = 0;
                }
                $_model['currentListRs'] = $this->repoArc->findProduct($_model['categoryId'], $user['member_id'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else if ($_model['channelType'] == 11) {
                $_model['currentCategoryTotal'] = $this->repoArc->findInviteTotal($_model['categoryId']);
                $_model['currentListRs'] = $this->repoArc->findInvite($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
//            var_dump($_model['currentListRs']);die;

            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][1]
            );
        }
        return 404;
    }

    public function detail($__Route = array('/{id:\d+}', '/college/detail/{id:\d+}', '/news/detail/{id:\d+}', '/customer/detail/{id:\d+}')) {
        $_GET['aliasId'] = $this->__RM__;
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $parent = explode('.', trim($_model['idTree'], '.'));
            $_model['parentId'] = $parent[1];
//            var_dump($_model['aliasId']);die;
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['substance'] = Html::outputToText($_model['substance']);

            if ($_model['channelType'] == 0) { //产品多图
                $_model['pics'] = $this->repoArc->productPics($this->id);
                $_model['link'] = $this->repoArc->productLink($this->id);
            }

            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][3]
            );
        }
        return 404;
    }

    public function invite($__Route = array('/invite/recruit/{id:\d+}/{page}')) {
        $this->aliasId = 'invite';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aliasId'] = $this->repoArc->aliasId($this->__LANGUAGE_ID__, 'invite/recruit/' . $this->id);
            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            $_model['currentCategoryTotal'] = $this->repoArc->findCurrentTotal($this->id);
            $_model['currentListRs'] = $this->repoArc->findCurrent($this->id,
                $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                $_model['currentPageSize']);

            return array(
                'model' => $_model,
                'view' => 'about/careersD'
            );
        }
        return 404;
    }

    public function search($__Route = array('/search')) {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aliasId'] = $this->repoArc->aliasId($this->__LANGUAGE_ID__, 'invite/recruit/' . $this->id);
            if (!$_GET['title']) {
                $_GET['title'] = '';
            }
            $_model['product'] = $this->repoArc->findProductAll(4, $_GET['title'], $this->__LANGUAGE_ID__);
            $_model['clinical'] = $this->repoArc->findProductAll(1, $_GET['title'], $this->__LANGUAGE_ID__);
            $_model['college'] = $this->repoArc->findProductAll(2, $_GET['title'], $this->__LANGUAGE_ID__);
            $_model['news'] = $this->repoArc->findProductAll(5, $_GET['title'], $this->__LANGUAGE_ID__);

            return array(
                'model' => $_model,
                'view' => 'search'
            );
        }
        return 404;
    }

    public function searchCollege($__Route = array('/searchCollege')) {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if (!$_GET['title']) {
                $_GET['title'] = '';
            }
            if (!$_GET['collegeTime']) {
                $_GET['collegeTime'] = 0;
            }
            if (!$_GET['type']) {
                $_GET['type'] = 0;
            }
            $_model['college'] = $this->repoArc->findCollegeAll(2, $_GET, $this->__LANGUAGE_ID__);

            return array(
                'model' => $_model,
                'view' => 'searchCollege'
            );
        }
        return 404;
    }


}
